#include "game.h"
#include "random.h"

void Game::initGame(int const & a_numPlayers)
{
	numPlayers = a_numPlayers;
	p = new Player[numPlayers];
	activePlayer = 0;
	p[activePlayer].setColor(Player::COLOR_SELECTED);
}

Game::Game()
{
	gameState = Game::MENU;
}

Game::~Game()
{
	delete [] p;
}

void Game::draw(SDL_Surface * screen)
{
	// clear the screen for all of the game
	SDL_FillRect(screen, 0, 0);			// black screen

	switch(gameState)
	{
	case Game::MENU:
		printf("I need to print a menu!!!\n");
		break;
	case Game::GAME:
		b.draw(screen);
		for(int i = 0; i < numPlayers; ++i)
		{
			p[i].draw(screen);
		}
		break;
	}

	// switch the back buffer
	SDL_Flip(screen);
}
void Game::update(int ms)
{
	switch(gameState)
	{
	case Game::MENU:
		break;
	case Game::GAME:
		for(int i = 0; i < numPlayers; ++i)
		{
			p[i].move(ms);
		}
//		b.update(ms);
		break;
	}
}

Uint32 timedFunction(Uint32 interval)
{
	printf("%d\n", interval);
	SDL_SetTimer(1500, 
		(SDL_TimerCallback)timedFunction);
	return 0;
}

void Game::menuInput(SDL_Event & e)
{
	switch(e.type)
	{
	case SDL_KEYDOWN:
		printf("going to game state!\n");
		int num = 0;
		switch(e.key.keysym.sym)
		{
		case SDLK_1:	num = 1;	break;
		case SDLK_2:	num = 2;	break;
		case SDLK_3:	num = 3;	break;
		case SDLK_4:	num = 4;	break;
		}
		if(num > 0)
		{
			initGame(num);
			gameState = Game::GAME;
			SDL_SetTimer(3000, 
				(SDL_TimerCallback)timedFunction);
		}
		break;
	}
}

/** 
 * @return index of the closest player to the given mouse click,
 * -1 if the click is too far from any players
 */
int Game::whichPlayerIsWithin(int const & x, int const & y, int radius)
{
	float minDistance, distance, dx, dy, maxRadius = (float)radius*radius;
	int closest = -1;
	for(int i = 0; i < numPlayers; ++i)
	{
		dx = p[i].location.x - x;
		dy = p[i].location.y - y;
		distance = dx*dx + dy*dy;
		if(distance < maxRadius 
		&& (closest < 0 || distance < minDistance))
		{
			minDistance = distance;
			closest = i;
		}
	}
	return closest;
}

/** @return true if a player was selected with the given mouse press */
bool Game::selectPlayer(int const & x, int const & y)
{
	// what am I pressing?
	int whichPlayer = whichPlayerIsWithin(x,y, 100);
	// is that a valid thing to press?
	if(whichPlayer >= 0)
	{
		// do my action
		p[activePlayer].setColor(Player::COLOR_UNSELECTED);
		activePlayer = whichPlayer;
		p[activePlayer].setColor(Player::COLOR_SELECTED);
		return true;
	}
	return false;
}

bool Game::mouseOverPlayer(int const & x, int const & y)
{
	// what am I pressing?
	int whichPlayer = whichPlayerIsWithin(x,y, 100);
	// is that a valid thing to press?
	if(whichPlayer >= 0)
	{
		// do my action
		p[whichPlayer].setColor(randomColor());
		return true;
	}
	return false;
}

void Game::gameInput(SDL_Event & e)
{
	switch(e.type)
	{
	case SDL_KEYDOWN:
		switch(e.key.keysym.sym)
		{
		case SDLK_UP:
		case SDLK_DOWN:
		case SDLK_LEFT:
		case SDLK_RIGHT:
			p[activePlayer].handleInput(e);
			break;
		case SDLK_SPACE:
			b.randomizeColors();
			p[activePlayer].setColor(Player::COLOR_UNSELECTED);
			activePlayer++;
			activePlayer %= numPlayers;
			p[activePlayer].setColor(Player::COLOR_SELECTED);
			break;
		}
		break;
	case SDL_MOUSEBUTTONDOWN:
		switch(e.button.button)
		{
		case SDL_BUTTON_LEFT:
			if(!selectPlayer(e.button.x, e.button.y))
			{
				b.handleInput(e);
			}
			break;
		}
		break;
	case SDL_MOUSEMOTION:
		mouseOverPlayer(e.motion.x, e.motion.y);
		break;
	}
}

void Game::input(SDL_Event & e)
{
	switch(gameState)
	{
	case Game::MENU:
		menuInput(e);
		break;
	case Game::GAME:
		gameInput(e);
		break;
	}
}
